/*1942 game clone
 * Authors:
 * Abdollah Tabareh
 * Bharat Bhushan Konka
 * Martin Svensson 
 * Richard Zianne
 * Tigran Harutyunyan
 * 
 * 2010
*/


using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Game1942
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class BossPlane : EnemyPlane
    {
        protected const short WIDTH = 98;
        protected const short HEIGHT = 98;
        protected TimeSpan _previousUpdate;
        protected const byte XYSpeed = 1;
        private int _countDown = 4;
        private int _prop = 0;
        public bool _active { get; set; }

        public BossPlane(Game game, ref Texture2D texture)
            : base(game)
        {
            _power = 100;
            _planeSpeed = 0;
            _position = new Vector2(-500,-500);
            _texture = texture;
            _planeWith = 96;
            _planeHight = 76;
            _active = false;
            _isDying = false;
            _isDead = true;

            //_spriteRectangle = new Rectangle(4, 70, _planeWith, _planeHight);
            _spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            Frames = new List<Rectangle>();
            Rectangle frame = new Rectangle();
            frame.X = 599;
            frame.Y = 14;
            frame.Width = _planeWith;
            frame.Height = _planeHight;
            Frames.Add(frame);//index 0 

            frame.X = 797;
            Frames.Add(frame);//index 1 
            frame.X = 698;
            Frames.Add(frame);//index 2
            frame.X = 797;
            Frames.Add(frame);//index 3 

            //Explosion
            frame.X = 4;
            frame.Y = 301;
            frame.Width = 65;
            frame.Height = 65;
            Frames.Add(frame);//index 4 
            frame.X = 70;
            Frames.Add(frame);//index 5
            frame.X = 136;
            Frames.Add(frame);//index 6
            frame.X = 202;
            Frames.Add(frame);//index 7
            frame.X = 268;
            Frames.Add(frame);//index 8
            frame.X = 334;
            Frames.Add(frame);//index 9
            frame.X = 400;
            Frames.Add(frame);//index 10

            _spriteRectangle = Frames.ElementAt(0);
            
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (_prop == 0)
            {
                _spriteRectangle = Frames.ElementAt(0);
                _prop++;
            }
            else if (_prop == 1)
            {
                _spriteRectangle = Frames.ElementAt(1);
                _prop++;
            }
            else if (_prop == 2)
            {
                _spriteRectangle = Frames.ElementAt(2);
                _prop++;
            }
            else if (_prop == 3)
            {
                _spriteRectangle = Frames.ElementAt(3);
                _prop = 0;
            }


            if (_isDying)
            {
                if (_countDown >= Frames.Count)
                {
                    _isDead = true;
                    putInPosition(-500, -500);
                    _countDown = 4;
                }
                else
                {
                    _spriteRectangle = Frames.ElementAt(_countDown);
                    _countDown++;
                }

            }

            if (!_isDead)
            {
                TimeSpan duration = gameTime.TotalGameTime.Subtract(_previousUpdate);
                if (duration.CompareTo(new TimeSpan(0, 0, 5)) > 0)//if more than 1 sec since previous update
                {
                    InitSpeed();
                    _previousUpdate = gameTime.TotalGameTime;
                }
                Move();
            }

            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {
            _spriteBatch.Begin();
            _spriteBatch.Draw(_texture, _position, _spriteRectangle, Color.White);
            _spriteBatch.End();
            base.Draw(gameTime);
        }
        public float getPositionX()
        {
            return _position.X;
        }
        public float getPositionY()
        {
            return _position.Y;
        }
        public void activateBoss()
        {
            _active = true;
            _isDead = false;
            _position = new Vector2(Util.ScreenWidth / 2,0);
            _planeSpeed = 2;
            
        }
        private void Move()
        {
            _position.X += _speed.X;
            _position.Y += _speed.Y;
            if (_position.X > Util.ScreenWidth - WIDTH)
                _speed.X = -1 * Math.Abs(_speed.X);
            if (_position.X < 0)
                _speed.X = Math.Abs(_speed.X);

            if (_position.Y > Util.ScreenWidth /2)
                _speed.Y = -1 * Math.Abs(_speed.Y);
            if (_position.Y < 0)
                _speed.Y = Math.Abs(_speed.Y);


        }

        private void InitSpeed()
        {
            int sign = (Util.Instance.RandomGenerator.Next(0, 2) > 0) ? 1 : -1;
            _speed.X = (Util.Instance.RandomGenerator.Next(XYSpeed) + 1) * sign;
            _speed.Y = Util.Instance.RandomGenerator.Next(XYSpeed) + 1;

        }

    }
}